package com.oa.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.server.entity.Admin;
import com.oa.server.entity.Menu;
import com.oa.server.mapper.MenuMapper;
import com.oa.server.service.IMenuService;
import com.oa.server.util.AdminUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcy
 * @since 2021-06-01
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Autowired
    MenuMapper menuMapper;
    @Override
    public List<Menu> getMenusByAdminId() {
        Integer adminId = AdminUtils.getCurrentAdmin().getId();
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        List<Menu> menuList = (List<Menu>)valueOperations.get("menu_" + adminId);
        System.out.println("menu_"+adminId);
      // redis 为空去数据库中查找
       if(CollectionUtils.isEmpty(menuList)){
           menuList = menuMapper.getMenusByAdminId(adminId);
           System.out.println("查询数据库");
           // 放入redis
           valueOperations.set("menu_"+adminId,menuList);
       }
        return menuList;
    }

    @Override
    public List<Menu> getMenuByRole() {
        return menuMapper.getMenuByRole();
    }

    @Override
    public List<Menu> getAllMenus() {

        return menuMapper.getAllMenus();
    }
}
